hysop.numerics.stencil.stencil_generator module¶
Finite differences stencil inteface used in various numerical methods.
- class hysop.numerics.stencil.stencil_generator.CenteredStencilGenerator(config=None, cache_override=False)[source]¶
Bases:
StencilGenerator
Generate various centered stencils. Results are cached and compressed locally.
StencilGenerator to generate stencils of dimension
dim
and typedtype
. Results are cached and compressed locally.- dtype¶
Datatype of the generated stencils.
- Type:
{np.float32, np.float64,
MPQ
}
- Parameters:
- Raises:
ValueError – Raised if dim<1 or if dtype in [np.float32, np.float64] and dim!=1.
TypeError – Raised if dtype is not a valid type.
Notes
Depending on data type
dtype
a different solver can be used. For fast generation of 1D stencils, use np.float32 or np.float64. For n-dimentional exact fractional coefficients stencils use the default mpq type. Exact fractional results (mpq) are cached locally.Examples
Generate 1D approximative forward first derivative stencil of order 1 and 2.
>>> import numpy as np >>> from hysop.numerics.stencil.stencil_generator import StencilGenerator >>> generator = StencilGenerator().configure(dim=1,dtype=np.float64) >>> s0 = generator.generate_approximative_stencil(origin=0, derivative=1, order=1) >>> s1 = generator.generate_approximative_stencil(origin=0, derivative=1, order=2) >>> print('{}\n{}'.format(s0.coeffs,s1.coeffs)) [-1. 1.] [-1.5 2. -0.5]
Generate 1D exact backward first derivative stencil of order 1 and 2.
>>> from hysop.numerics.stencil.stencil_generator import StencilGenerator >>> generator = StencilGenerator().configure(dim=1) >>> s0 = generator.generate_exact_stencil(origin=-1, derivative=1, order=1) >>> s1 = generator.generate_exact_stencil(origin=-1, derivative=1, order=2) >>> print('{}\n{}'.format(s0.coeffs,s1.coeffs)) [-1 1] [1/2 -2 3/2]
Generate centered 2D Laplacian of order 2:
>>> import sympy as sm >>> from hysop.numerics.stencil.stencil_generator import StencilGenerator >>> generator = StencilGenerator().configure(dim=2, derivative=2) >>> laplacian = generator.generate_exact_stencil(origin=1, order=2, dx=(sm.Symbol('dx'),)*2) >>> print(laplacian.coeffs) [[0 1 0] [1 -4 1] [0 1 0]]
Similarly, with different custom dx:
>>> laplacian = generator.generate_exact_stencil(origin=1, order=2) >>> print(laplacian.coeffs) [[0 dx**(-2) 0] [dy**(-2) (-2*dx**2 - 2*dy**2)/(dx**2*dy**2) dy**(-2)] [0 dx**(-2) 0]]
See also
Stencil
- class hysop.numerics.stencil.stencil_generator.StencilGenerator(config=None, cache_override=False)[source]¶
Bases:
object
Generate various stencils. Results are cached and compressed locally.
StencilGenerator to generate stencils of dimension
dim
and typedtype
. Results are cached and compressed locally.- dtype¶
Datatype of the generated stencils.
- Type:
{np.float32, np.float64,
MPQ
}
- Parameters:
- Raises:
ValueError – Raised if dim<1 or if dtype in [np.float32, np.float64] and dim!=1.
TypeError – Raised if dtype is not a valid type.
Notes
Depending on data type
dtype
a different solver can be used. For fast generation of 1D stencils, use np.float32 or np.float64. For n-dimentional exact fractional coefficients stencils use the default mpq type. Exact fractional results (mpq) are cached locally.Examples
Generate 1D approximative forward first derivative stencil of order 1 and 2.
>>> import numpy as np >>> from hysop.numerics.stencil.stencil_generator import StencilGenerator >>> generator = StencilGenerator().configure(dim=1,dtype=np.float64) >>> s0 = generator.generate_approximative_stencil(origin=0, derivative=1, order=1) >>> s1 = generator.generate_approximative_stencil(origin=0, derivative=1, order=2) >>> print('{}\n{}'.format(s0.coeffs,s1.coeffs)) [-1. 1.] [-1.5 2. -0.5]
Generate 1D exact backward first derivative stencil of order 1 and 2.
>>> from hysop.numerics.stencil.stencil_generator import StencilGenerator >>> generator = StencilGenerator().configure(dim=1) >>> s0 = generator.generate_exact_stencil(origin=-1, derivative=1, order=1) >>> s1 = generator.generate_exact_stencil(origin=-1, derivative=1, order=2) >>> print('{}\n{}'.format(s0.coeffs,s1.coeffs)) [-1 1] [1/2 -2 3/2]
Generate centered 2D Laplacian of order 2:
>>> import sympy as sm >>> from hysop.numerics.stencil.stencil_generator import StencilGenerator >>> generator = StencilGenerator().configure(dim=2, derivative=2) >>> laplacian = generator.generate_exact_stencil(origin=1, order=2, dx=(sm.Symbol('dx'),)*2) >>> print(laplacian.coeffs) [[0 1 0] [1 -4 1] [0 1 0]]
Similarly, with different custom dx:
>>> laplacian = generator.generate_exact_stencil(origin=1, order=2) >>> print(laplacian.coeffs) [[0 dx**(-2) 0] [dy**(-2) (-2*dx**2 - 2*dy**2)/(dx**2*dy**2) dy**(-2)] [0 dx**(-2) 0]]
See also
Stencil
- CROSS = 1¶
- CUSTOM = 99¶
- DENSE = 0¶
- DIAG = 2¶
- configure(config=None, **kargs)[source]¶
Push a new stencil generator configuration by keyword arguments (see StencilGeneratorConfiguration.configure() or by StencilGeneratorConfiguration instance.
- generate_approximative_stencil(origin, **kargs)[source]¶
Generate a stencil using hardware floating point arithmetic (fast). dtype can be np.float16, np.float32, np.float64, MPQ